/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.impl;

import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.RegistroDao;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil;
import java.util.List;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dominio.Registro;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author mauriljb
 */
public class RegistroDaoImpl implements RegistroDao {
    
    @Override
     public List<Registro> getAll() {
        Session session = ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Registro.class);
            criteria.createAlias("huesped", "huesped");
            criteria.addOrder(Order.asc("registroId"));
            List<Registro> lista = criteria.list();
            session.close();
            return lista;
     }
     
    @Override
      public Registro getHuesped(long id) {
       Session session = HibernateUtil.getSessionFactory().openSession();
       session.beginTransaction();
       Criteria criteria = session.createCriteria(Registro.class);
       criteria.add(Restrictions.eq("id", id));
       Registro registro = (Registro) criteria.list().get(0);
       session.close();
       return registro;
       }
    
    @Override
       public void insert(Registro registro) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(registro);
            session.getTransaction().commit();
            session.close();
    }
    @Override
          public void delte(Registro registro) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(registro);
            session.getTransaction().commit();
            session.close();
    }
          
    @Override
             public void update(Registro registro) {
          Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(registro);
            session.getTransaction().commit();
            session.close();
    }
}
